﻿using MediatR;
using W.Purchase.Domain;
using W.Purchase.Read.API.AppLication.Command.Command;
using W.Purchase.Read.API.AppLication.CommandHandler;
using W.Purchase.Repository.Impl;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.Command.CommandHandler
{
    public class ClientNameShowCommandHandler : IRequestHandler<ClientNameShowCommand, ApiResult<List<Client>>>
    {
        private readonly IClientRepository _repository;
        private readonly ILogger<ClientNameShowCommandHandler> _logger;
        public ClientNameShowCommandHandler(IClientRepository repository, ILogger<ClientNameShowCommandHandler> logger)
        {
            _repository = repository;
            _logger = logger;
        }
        public async Task<ApiResult<List<Client>>> Handle(ClientNameShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<Client>> result = new ApiResult<List<Client>>();
            try
            {
                _logger.LogInformation("显示客户名称成功");
                var list = await _repository.GetAsync(x => x.IsDelete == false);
                result.Data = list;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
